package com.fofo.test.caculator.common;

/**
 * 实现栈的基本结构和特性
 */
public class StackArray {

    private int size;
    private String[] stackArray;
    private int index = -1;

    public StackArray(int size){
        this.size = size;
        stackArray = new String[size];
    }

    public boolean empty(){
        if ( index == -1 ) {
            return true;
        }
        return false;
    }

    public boolean full(){
        if( index == size-1 ){
            return true;
        }
        return false;
    }

    public void push (String num) {
        if ( full() ) {
            System.out.println("stack is full.");
            return;
        } else {
            index++;
            stackArray[index] = num;
        }
    }

    public String pop () {
        if ( empty() ) {
            System.out.println("stack is empty.");
            return null;
        } else {
            String value = stackArray[index];
            index--;
            return value;
        }
    }

    public void printStack() {
        if (empty()) {
            System.out.println("the stack is empty");
        } else {
            int j = index;
            for (int i = j; i > -1; i--) {
                System.out.println(stackArray[i]);
            }
        }
    }

    public void printArray() {
        if (empty()) {
            System.out.println("the array is empty");
        } else {
            int j = index;
            for (int i = 0; i < index + 1; i++) {
                System.out.print(stackArray[i]);
            }
        }
    }

    public String getStackTop(){
        return stackArray[index];
    }
}











